package com.fyyg.common.util;

/**
 * 简易分页工具类
 * @author zdx
 * 使用此类的时候，必须需要先调用init方法，默认显示条数为50（可设），默认显示5页（可设）
 * 格式：1...3 4 5 6 7 8...100
 * 2013.1.11
 */

/**
 * 说明：
 * javascript:链接页码默认的js方法为toPage(1); 必须有此方法才有效果
 * 例：function toPage(p){
		alert(p);
	  }
 *	  
 * CSS:此分页需配置以下样式，
 * html代码："<div class='fralign' id='pageCss'>p.pageStr</div>"
 * #pageCss a{display:block; border:1px solid #aaaeb9; padding:0px 8px 0px 8px; font-size:12px; height:20px; line-height:20px; text-decoration:none; margin:0px 4px 0px 4px; float:left; color:#51596e;}
 * #pageCss .focusLink{display:block; border:1px solid #6b8caf; background-color:#cce6ff; padding:0px 8px 0px 8px; font-size:12px; height:20px; line-height:20px; text-decoration:none; margin:0px 4px 0px 4px; float:left; _display:inline; color:#51596e; font-weight:bold;}
 * #pageCss .Ellipsis{display:block; border:0px solid #6b8caf;  padding:0px 8px 0px 8px; font-size:12px; height:20px; line-height:20px; text-decoration:none; margin:0px 4px 0px 4px; float:left; color:#51596e;}
 * .fralign {float:right;_display:inline;}
 * 
 */

public class PageBean {
//------------------------------参数属性-----------------------------------------------------------	
	private int pageSize = 50;// 每页显示的条数
	private int pageCount;// 总页数
	private int sum;// 总数据个数
	private int noPage = 1;// 当前页码
	private int lastPageNum = 0; //上一页码
	private int nextPageNum = 0; //下一页码
	private int statepage = 0; //查询的起始条数
	private int endpage = 0; //查询的结束条数
	private int nums = 4;// 每组分页显示的个数：如果要显示5，设置的时候要小一位，设成4
	private int lastPage = 0; //是否有上一页 0表示无上一页 1表示有
	private int nextPage = 0; //是否有下一页 0表示无下一页 1表示有
	private int showStratPage = 1; //显示间隔的起始页
	private int showEndPage = 1; //显示间隔的结束页
	public String pageStr = "";// 返回每页的分页数字字符串
	
//----------------------------取得参数--------------------------------------------------------------
	public int getPageSize() {  //获取每页大小
		return pageSize;
	}
	public int getSum() { //取得每页总数
		return sum;
	}
	public int getNoPage() { //取得当前页
		return noPage;
	}
	public int getPageCount() { //取得最大页数
		return pageCount;
	}
	public int getLastPageNum() {
		return lastPageNum;
	}
	public int getNextPageNum() {
		return nextPageNum;
	}
	
	public int getLastPage() {
		if(lastPage > pageCount ){
			return pageCount;
		}else{
			return lastPage;
		}
	}
	public int getNextPage() {
		return nextPage;
	}
	public String getPageStr(){
		return pageStr;
	}
	public int getStatepage() {
		return statepage;
	}
	public int getEndpage() {
		return endpage;
	}
	public int getNums() {
		return nums;
	}
	public int getShowStratPage() {
		return showStratPage;
	}
	public int getShowEndPage() {
		return showEndPage;
	}
	//-----------------------------设置参数-----------------------------------------------------------------	
	
	
	//设置每页显示条数
	public void setPageSize(int pageSize) { 
		this.pageSize = pageSize;
	}
	
	//设置当前页 上一页 下一页
	public void setNoPage(int noPage){ 
		this.noPage = noPage;
		if(noPage > 1){
			this.lastPage = 1;
			this.lastPageNum = noPage-1;
		}else{
			this.lastPage = 0;
			this.lastPageNum = 0;
		}
		if(pageCount>noPage){
			this.nextPage = 1;
			this.nextPageNum = noPage+1;
		}else{
			this.nextPage = 0;
			this.nextPageNum = noPage;
		}
	}
	
	//设置需要显示分页多少页数，默认为5
	public void setNums(int nums) {
		this.nums = nums-1;
	}
	
	//设置显示多少页
	public void initShowPage(int noPage){
		if((noPage-nums/2)>2){  //1...3 4 5 6 7
			this.showStratPage = noPage-2;
		}else{
			this.showStratPage = 1;
		}
		if(showStratPage+nums < pageCount){
			showEndPage = showStratPage+nums;
		}else{
			showEndPage = pageCount;
		}
	}
	
	/**
	 * 计算总页数
	 * @param pagesum 总条数
	 */
	private void setPageCount(int pagesum) {
		this.sum = pagesum;
		if (pagesum <= this.pageSize) { //如果小于每页显示，总页数为1
			this.pageCount = 1;
		} else {
			this.pageCount = (pagesum - 1) / this.pageSize + 1;
		}
	}
	
//----------------------------------------初始化方法-------------------------------------------------------------	
	
	/**
	 * 初使化
	 * @param sum 数据总量
	 * @param pagenum 当前页数
	 */
	public void initPage(int sum,int noPage){
		this.setPageCount(sum); //计算最大页
		this.setNoPage(noPage);
		if(noPage < 2){ 
			this.statepage = 0;
			this.endpage = this.getPageSize();
		}else{
			this.statepage = (noPage-1)*this.getPageSize();
			this.endpage = noPage*this.getPageSize();
		}
		initShowPage(noPage);
		this.setPageStr();
	}
//-------------页面显示样式-------------------------------------------------------------------------
	//分隔点
	public String strCode = "<span style='float:left;'>…</span>";
	//普通页样式
	public String cssStrStrat1 = "<a href='javascript:void(0)' style='border:1px solid #aaaeb9;color:#51596e;' onclick='toPage(";
	//当前页样式
	public String cssStrStrat2 = "<a href='javascript:void(0)' style='border:1px solid #aaaeb9;background-color:#cce6ff; color:#51596e; font-weight:bold;' class='focusLink' onclick='toPage(";
	public String sEnd = ")'>";
	public String cssStrEnd = "</a>";
	
	//匹配字符窜
	public void setPageStr(){
		if(pageCount <= nums+2){  // 1 2 3 4 5
			for(int i=1; i<pageCount+1; i++){
				if(i==noPage){ //当前页
					pageStr =pageStr + cssStrStrat2+ i + sEnd + i + cssStrEnd;
				}else{
					pageStr =pageStr + cssStrStrat1+ i + sEnd + i + cssStrEnd;
				}
			}
		}else{
			//判断是否要加前面部分：例1...3 4 5 6
			if(showStratPage != 1){
				pageStr =cssStrStrat1 + "1" + sEnd + "1" + cssStrEnd + strCode;
			}
			
			//循环中间部分1...3 4 5 6 7...9
			for(int j=showStratPage; j<=showEndPage; j++){
				if(j==noPage){ //当前页
					pageStr =pageStr + cssStrStrat2+ j + sEnd + j + cssStrEnd;
				}else{
					pageStr =pageStr + cssStrStrat1+ j + sEnd + j + cssStrEnd;
				}
			}
			
			//判断直接加末尾页或加...末尾页：例：1...3 4 5 6 7...9 or 1...3 4 5 6 7 8 小于的情况不需要判断
			if(pageCount==noPage+nums/2+1){  // 直接加
				pageStr =pageStr + cssStrStrat1+ pageCount + sEnd + pageCount + cssStrEnd;
			}else if(pageCount>noPage+nums/2+1){  //判断后面加 ... 8
				pageStr =pageStr+strCode + cssStrStrat1+ pageCount + sEnd + pageCount + cssStrEnd;
			}
		}
	}
	
}
